package com.daiji.controller;

import com.daiji.dao.impl.StudentDaoImpl;
import com.daiji.dao.impl.UserDaoImpl;
import com.daiji.entity.UserEntity;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/user")
public class UserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        UserDaoImpl userDao = new UserDaoImpl();
        UserEntity user = userDao.select(username, password);

        if (user!=null){
            HttpSession session = req.getSession();
            session.setAttribute("user",user);
            req.getRequestDispatcher("/index.jsp").forward(req,resp);
        }else {
            req.setAttribute("msg","账户名或密码错误");
            req.getRequestDispatcher("/fail.jsp").forward(req,resp);
        }

    }
}
